﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Basic4Net.BusinessRules;
using Basic4Net.OAWeb.Base;
using System.Data;
using Basic4Net.Utility.Common;

namespace Basic4Net.OAWeb.Module_Report
{
    public partial class WholeYearFinanceReport : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                int Year = DateTime.Today.Year;

                ddl_Year.Items.Add(new ListItem((Year).ToString(), (Year).ToString()));
                ddl_Year.Items.Add(new ListItem((Year - 1).ToString(), (Year - 1).ToString()));
                ddl_Year.Items.Add(new ListItem((Year - 2).ToString(), (Year - 2).ToString()));


                GridBind();
            }
        }

        public double? PayAmount = 0;
        public double? AppAmount = 0;

        public void GridBind()
        {
			//int Type = Convert.ToInt32(Request["Type"]);
			//ReportYearBR pReportYearBR = new ReportYearBR();

			//object list = pReportYearBR.GetList(Convert.ToInt32(ddl_Year.SelectedValue), Type, ref PayAmount, ref AppAmount);

			//pRepeater.DataSource = list;
			//pRepeater.DataBind();

			int Type = Convert.ToInt32(Request["Type"]);
			ReportYearBR br = new ReportYearBR();
			var list = br.GetList(ExistSuper((int)(Session["UserID"] ?? -1)), (int)(Session["UserID"] ?? -1), Convert.ToInt32(ddl_Year.SelectedValue), Type, ref PayAmount, ref AppAmount);

			DataTable dt = new DataTable();
			Common.IListToTable(dt, new WholeYearFinanceReportShow());
			var mylist = list;
			foreach (var mylisti in mylist)
			{
				DataRow row = dt.NewRow();
                row["UserID"] = mylisti["UserID"];
				row["UserName"] = mylisti["UserName"];
				row["UserCode"] = mylisti["UserCode"];
                //row["Money1"] = mylisti["Money1"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money1"].ToString()), 2) : mylisti["Money1"];
                //row["Money2"] = mylisti["Money2"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money2"].ToString()), 2) : mylisti["Money2"];
                //row["Money3"] = mylisti["Money3"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money3"].ToString()), 2) : mylisti["Money3"];
                //row["Money4"] = mylisti["Money4"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money4"].ToString()), 2) : mylisti["Money4"];
                //row["Money5"] = mylisti["Money5"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money5"].ToString()), 2) : mylisti["Money5"];
                //row["Money6"] = mylisti["Money6"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money6"].ToString()), 2) : mylisti["Money6"];
                //row["Money7"] = mylisti["Money7"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money7"].ToString()), 2) : mylisti["Money7"];
                //row["Money8"] = mylisti["Money8"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money8"].ToString()), 2) : mylisti["Money8"];
                //row["Money9"] = mylisti["Money9"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money9"].ToString()), 2) : mylisti["Money9"];
                //row["Money10"] = mylisti["Money10"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money10"].ToString()), 2) : mylisti["Money10"];
                //row["Money11"] = mylisti["Money11"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money11"].ToString()), 2) : mylisti["Money11"];
                //row["Money12"] = mylisti["Money12"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money12"].ToString()), 2) : mylisti["Money12"];
                //row["Money13"] = mylisti["Money13"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["Money13"].ToString()), 2) : mylisti["Money13"];
                //row["MoneyTotal"] = mylisti["MoneyTotal"].GetType().ToString() != "System.DBNull" ? (float)Math.Round(Convert.ToDouble(mylisti["MoneyTotal"].ToString()), 2) : mylisti["MoneyTotal"];
                row["Money1"] = mylisti["Money1"];
                row["Money2"] = mylisti["Money2"];
                row["Money3"] = mylisti["Money3"];
                row["Money4"] = mylisti["Money4"];
                row["Money5"] = mylisti["Money5"];
                row["Money6"] = mylisti["Money6"];
                row["Money7"] = mylisti["Money7"];
                row["Money8"] = mylisti["Money8"];
                row["Money9"] = mylisti["Money9"];
                row["Money10"] = mylisti["Money10"];
                row["Money11"] = mylisti["Money11"];
                row["Money12"] = mylisti["Money12"];
                row["Money13"] = mylisti["Money13"];
                row["MoneyTotal"] = mylisti["MoneyTotal"];
				dt.Rows.Add(row);
			}

			//------------------------------------------------------------------------分页处理开始......
			int PageNow = 0;
			DataView dv = dt.DefaultView;
			System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
			ps.DataSource = dv;
			ps.AllowPaging = true;
			ps.PageSize = 20;
			if (this.txt_Go.Text.ToString() == "")
				this.txt_Go.Text = "1";
			if (this.txt_Go.Text.ToString() == "0")
				this.txt_Go.Text = "1";
			if (Convert.ToInt32(this.txt_Go.Text.ToString()) >= ps.PageCount)
				this.txt_Go.Text = ps.PageCount.ToString();
			PageNow = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;

			ps.CurrentPageIndex = Convert.ToInt32(this.txt_Go.Text.ToString()) - 1;
			//-------------------------------------------------------------------------分页处理结束

			pRepeater.DataSource = ps;
			pRepeater.DataBind();

			//------------------------------------------------------------------------记录总数、当前页数、总页数处理开始......
			//this.lbl_SumCount.Text = ds.Tables[0].Rows.Count.ToString();
			this.lbl_ToPage.Text = this.txt_Go.Text.ToString();
			this.lbl_PageCount.Text = ps.PageCount.ToString();

			//------------------------------------------------------------------------------记录总数、当前页数、总页数处理结束
		}

		protected void btnBind_Click(object sender, EventArgs e)
		{
			GridBind();
		}

        protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridBind();
        }

        protected string GetValue(string value)
        {
            if (value != "")
            {
                return Math.Round(Convert.ToDouble(value), 2).ToString();
            }
            else
            {
                return value;
            }
        }
    }
}